package org.azj.dbOperatorComponent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;

import org.azj.logHelper.LOG;
import org.azj.logHelper.LOG_COMPONENT;
import org.azj.logHelper.LOG_LEVEL;



public abstract class DbNonQueryCommand implements ICommand
{
	abstract void doExecute(PreparedStatement preparedStatement) throws SQLException;
	
	@Override
	public List<HashMap<String, Object>> doCommand(DataSource ds) throws SQLException
	{
		Connection conn = SingleThreadConnectionHolder.getConnection(ds);
		PreparedStatement statement = null;

		try {
			statement = getStatement(conn);
		} catch (Exception e) {
			LOG.WriteLog(
					LOG_LEVEL.OPERATIONAL, 
					LOG_COMPONENT.LOG_COMP_DB_CHANGE, 
					"执行数据库任务时出现问题 : " + e.getMessage());
			throw new SQLException(e.getMessage());
		}
		
		doExecute(statement);
		statement.close();
		
		// 对于非查询类动作，直接返回一个空的ResultSet
		return new ArrayList< HashMap<String, Object> >(); 	
	}	
}
